package com.apestech.framework.db;

import com.apestech.framework.sql.SqlUtil;
import com.apestech.framework.xml.sql.modul.QueryRule;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Locale;

/**
 * @author hup
 * @create 2018-10-15-14:45
 */
public class Format extends SqlUtil{

    public class FormatField {
        private String smath=";ceil;floor;round;";
        private String scount=";sum;avg;min;max;";
        private int scale=2;  //小数位，默认2
        private String math=null;  //公式（ceil：向上取整1.1为2，  floor：向下取整1.1为1，  round：四舍五入不解释），默认round
        private String type="number";  //类型 （number数值类型， amount金额类型，percent百分比，permillage千分比，timeStamp：时间戳字串格式）默认number
        private Locale locale;
        private String fieldCount;  //合计公式 sum% sum, avg, max, min
        private boolean single;

        public FormatField(QueryRule.OutputField out, Locale locale) throws Exception {
            if(out==null) return;
            this.type=out.getType();
            if(out.getFormat()!=null){
                SqlUtil.MathField mathfield=getMathField(out.getFormat());
                String param1=mathfield.getParam1();
                String param2=mathfield.getParam2();
                if(smath.indexOf(";"+param1+";")>=0){
                    this.math=param1;
                    this.single=true;
                } else if(scount.indexOf(";"+param1+";")>=0){
                    this.fieldCount=param1;
                    this.single=false;
                }
                if(param2!=null){
                    if(smath.indexOf(";"+param2+";")>=0){
                        this.math=param2;
                        this.single=true;
                    } else if(scount.indexOf(";"+param2+";")>=0){
                        this.fieldCount=param2;
                        this.single=false;
                    }
                }
                if(out.getScale()!=null){
                    if(!isPureDigital(out.getScale())){
                        throw new Exception("检查配置信息，括号里的值必须为正整数!实际值是["+out.getScale()+"]");
                    }
                    this.scale=Integer.parseInt(out.getScale());
                }
                if(this.math!=null){
                    if(!"round".equals(this.math) && !"ceil".equals(this.math) && !"floor".equals(this.math) ){
                        throw new Exception("公式值范围必须是[round, ceil, floor]，实际值是["+math+"]");
                    }
                }
                if(this.fieldCount!=null){
                    if(!"sum".equals(this.fieldCount) && !"avg".equals(this.fieldCount)
                            && !"max".equals(this.fieldCount)&& !"min".equals(this.fieldCount) ){
                        throw new Exception("公式值范围必须是[sum, avg, max, min]，实际值是["+fieldCount+"]");
                    }
                }

            }
            if("timeStamp".equals(this.type) ){
                this.math=null;
            } else if(this.math==null && this.type!=null){
                this.math="round";
            }
            this.locale=locale;
        }

        public FormatField(String type) throws Exception {
            this.type=type;
        }

        public int getScale() {
            return scale;
        }

        public String getMath() {
            return math;
        }

        public void setMath(String math) {
            this.math = math;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Locale getLocale() {
            return locale;
        }

        public boolean isSingle() {
            return single;
        }

        public String getFieldCount() {
            return fieldCount;
        }
    }

    private Hashtable<String, FormatField> format=new Hashtable<String, FormatField>();
    private ArrayList<String> klist;
    private int size=-1;

    public Format(){}

    public Format(QueryRule rule) throws Exception {
        Locale locale=iniLocale(rule.getLocal());
        klist=rule.getOutKeyList();
        size=klist.size();
        for(int i=0; i<size; i++){
            String key=klist.get(i);
            format.put(key, new FormatField(rule.getOutById(key), locale));
        }
    }

    public Locale iniLocale(String l){
        if(l!=null){
            switch (l){
                case "cn":return Locale.CHINA;
                case "en":return Locale.ENGLISH;
                case "fr":return Locale.FRENCH;
                case "de":return Locale.GERMAN;
                case "it":return Locale.ITALIAN;
                case "ja":return Locale.JAPANESE;
                case "ko":return Locale.KOREAN;
                case "zh":return Locale.SIMPLIFIED_CHINESE;
                case "tw":return Locale.TRADITIONAL_CHINESE;
                case "gb":return Locale.UK;
                case "us":return Locale.US;
                case "ca":return Locale.CANADA;
                case "frca":return Locale.CANADA_FRENCH;
                default:return Locale.getDefault();
            }
        }
        return Locale.getDefault();
    }

    public int size(){
        return size;
    }

    public ArrayList<String> getKeylist() {
        return klist;
    }

    public FormatField getFieldById(String id){
        return format.get(id);
    }

    public int getSize() {
        return size;
    }
}
